package com.jindi.jacococli.biz.business.service;

import com.jindi.infra.tools.thread.EnhanceThreadPoolExecutor;
import com.jindi.jacococli.biz.config.AppProperties;
import com.jindi.jacococli.biz.constant.Constant;
import com.jindi.jacococli.biz.utils.ProcessUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;

@Service
public class SonarService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SonarService.class);

    @Resource
    private AppProperties appProperties;

    @Resource
    private EnhanceThreadPoolExecutor sonarTaskThreadPool;

    public void executeSonar(String path, String projectName) {
        sonarTaskThreadPool.execute(() -> {
            try {
                String command = String.format(Constant.SONAR_COMMAND, projectName, appProperties.getBranch(projectName));
                LOGGER.info("sonar command:{}", command);
                boolean result = ProcessUtils.execCommand(command, new File(path));
                LOGGER.info("sonar command result:{}", result);
            } catch (Exception e) {
                LOGGER.error("sonar command error:{}", e.getMessage());
            }
        });
    }

}
